package com.bzframework.pro.authorize.strategy.login;

import com.bzframework.pro.authorize.token.UserToken;
import lombok.RequiredArgsConstructor;
import org.hswebframework.web.exception.BusinessException;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @Author bingzhong
 * @Date 2024/12/13 15:19
 * @Description 登录策略执行器
 */
@RequiredArgsConstructor
public class LoginStrategyExecutor {

    private final List<LoginStrategy> loginStrategies;

    /**
     *
     * @param parameter 携带参数
     * @return usertoken
     */
    public Mono<UserToken> doLogin(Map<String, Object> parameter) {

        String loginType = String.valueOf(parameter.getOrDefault("loginType", ""));
        Assert.hasText(loginType, "登录类型(loginType)不能为空");
        LoginTypeEnum loginTypeEnum = LoginTypeEnum.valueOf(loginType);

        for (LoginStrategy strategy : loginStrategies) {

            if (strategy.getClass().isAnnotationPresent(LoginStrategyConfig.class)) {
                LoginStrategyConfig annotation = strategy.getClass().getAnnotation(LoginStrategyConfig.class);
                if (annotation.strategy().equals(loginTypeEnum)) {
                    return strategy.signIn(parameter);
                }
            }
        }
        return Mono.error(new BusinessException("登录失败"));
    }


}
